package com.powernode.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.clients.IndexImgProdFeign;
import com.powernode.constant.IndexImgConstant;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Prod;
import com.powernode.mapper.IndexImgMapper;
import com.powernode.service.IndexImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService{
    @Autowired
    private IndexImgMapper indexImgMapper;

    @Autowired
    private IndexImgProdFeign indexImgProdFeign;

    /**
     * 查询前台轮播图
     * @return
     */
    @Override
    @Cacheable(key = IndexImgConstant.FRONT_INDEX_IMG_LIST)
    public List<IndexImg> selectFrontIndexImg() {

        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
                .eq(IndexImg::getStatus, 1)
                .orderByDesc(IndexImg::getSeq));
    }

    @Override
    public void selectIndexImgPage(Page<IndexImg> page, IndexImg indexImg) {
        indexImgMapper.selectPage(page, new LambdaQueryWrapper<IndexImg>()
            .eq(ObjectUtil.isNotEmpty(indexImg.getStatus()), IndexImg::getStatus, indexImg.getStatus())
                .orderByDesc(IndexImg::getSeq)
        );
    }

    @Override
    @CacheEvict(key = IndexImgConstant.FRONT_INDEX_IMG_LIST)
    public boolean save(IndexImg entity) {
        // 1. 获取类型
        final Integer type = entity.getType();
        final Integer status = entity.getStatus();

        // 2. 没有关联商品
        if (type == -1) {
            entity.setRelation(-1L);
        }

        // 3. 状态为1则
        if (1 == status) {
            entity.setUploadTime(new Date());
        }

        return indexImgMapper.insert(entity) > 0;
    }

    @Override
    public IndexImg getById(Serializable id) {
        // 1. 查询轮播图信息
        IndexImg indexImg = indexImgMapper.selectById(id);

        // 2. 获取轮播图类型
        Integer type = indexImg.getType();

        // 3. 如果关联了，则获取商品的信息
        if (0 == type) {
            // 远程调用
            Long prodId = indexImg.getRelation();
            Prod prod = indexImgProdFeign.getProdById(prodId);
            indexImg.setPic(prod.getPic());
            indexImg.setProdName(prod.getProdName());
        }
        return indexImg;
    }


    @Override
    @CacheEvict(key = IndexImgConstant.FRONT_INDEX_IMG_LIST)
    public boolean updateById(IndexImg entity) {
        if (entity.getStatus() == 1) {
            entity.setUploadTime(new Date());
        }

        if (entity.getType() == -1) {
            entity.setRelation(-1L);
        }
        return indexImgMapper.updateById(entity) > 0;
    }


    @Override
    @CacheEvict(key = IndexImgConstant.FRONT_INDEX_IMG_LIST)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return indexImgMapper.deleteBatchIds(idList) > 0;
    }
}
